//
//  PersonListViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/3.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "PersonListViewController.h"
#import "HXMacro.h"
#import "PersonListCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "PersonListModel.h"
#import "PersonViewController.h"
#import "PersonListRes.h"

#define kPersonListCollectionViewCellName @"PersonListCollectionViewCellNameReuse"
@interface PersonListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) PersonListModel *model;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomDistance;


@end

@implementation PersonListViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    // 改变约束 隐藏tabbar 那个白条
    _bottomDistance.constant = -49;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"人物志";
    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Pop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = back;
    self.navigationItem.rightBarButtonItem = search;
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) / 2, (SCREEN_WIDTH - 30) / 2 + 100);
    _layout.minimumInteritemSpacing = 10;
    _layout.minimumLineSpacing = 10;
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"PersonListCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kPersonListCollectionViewCellName];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"6"};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=master&a=getlist" andBody:@"page=1" andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [PersonListModel modelWithDic:result];
        [_collectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
        
    }];
}
- (void)click:(UIBarButtonItem *)button {
    
}

- (void)back:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _model.res.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PersonListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPersonListCollectionViewCellName forIndexPath:indexPath];
    cell.res = _model.res[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PersonViewController *vc = [[PersonViewController alloc] init];
    vc.numString = [_model.res[indexPath.row] personList_id];
    [self.navigationController pushViewController:vc animated:YES];
}
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
